package com.molyfun.parents.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseMainTabFragment;
import com.molyfun.parents.push.PushMsgCacheCenter;
import com.molyfun.parents.rest.bean.UserResponse;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.utils.TimeUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:9/19/16.
 * 主页
 */
public class MainMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 1001;
    private static final int TYPE_CATEGORY = 1002;
    public static final int MAIN_TAB_MSG_POSITION = 11;
    private Context mContext;
    private List<MainMenuEntity> mDataList;
    private MainMenuCallBack mMainMenuCallBack;
    private int mCurrentPosition = 2;

    public MainMenuAdapter(Context mContext, List<MainMenuEntity> list, MainMenuCallBack mainMenuCallBack) {
        this.mContext = mContext;
        this.mDataList = list;
        this.mMainMenuCallBack = mainMenuCallBack;
    }

    public static class MainMenuEntity {
        BaseMainTabFragment mFragmentClass;
        int mMenuName;
        int mMenuIcon;

        public MainMenuEntity(BaseMainTabFragment fragment, int menuName, int menuIcon) {
            this.mFragmentClass = fragment;
            this.mMenuName = menuName;
            this.mMenuIcon = menuIcon;
        }

        public BaseMainTabFragment getmFragment() {
            return mFragmentClass;
        }

        public void setmFragment(BaseMainTabFragment mFragmentClass) {
            this.mFragmentClass = mFragmentClass;
        }

        public int getmMenuName() {
            return mMenuName;
        }

        public void setmMenuName(int mMenuName) {
            this.mMenuName = mMenuName;
        }

        public int getmMenuIcon() {
            return mMenuIcon;
        }

        public void setmMenuIcon(int mMenuIcon) {
            this.mMenuIcon = mMenuIcon;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            return new TopCategotyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_main_header, parent, false));
        } else {
            return new CategoryViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_main_menu_cell, parent, false));
        }
    }

    public void refreshNoticeCount() {
        notifyItemChanged(MAIN_TAB_MSG_POSITION);
        //刷新用户数据
        notifyItemChanged(0);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEADER:
                TopCategotyViewHolder topCategotyViewHolder = (TopCategotyViewHolder) holder;
                if (MFPApplication.getInstance().getUser() != null) {
                    if (!TextUtils.isEmpty(MFPApplication.getInstance().getUser().getImgurl())) {
                        Picasso.with(mContext)
                                .load(MFPApplication.getInstance().getUser().getImgurl())
                                .config(Bitmap.Config.RGB_565)
                                .placeholder(R.mipmap.ic_header_default)
                                .into(topCategotyViewHolder.mAvator);
                    } else {
                        topCategotyViewHolder.mAvator.setImageResource(R.mipmap.ic_header_default);
                    }
//                    topCategotyViewHolder.mUserName.setText(MFPApplication.getInstance().getUser().getStudentname() + "(" + MFPApplication.getInstance().getUser().getStudentremarkname() + ")");
                    topCategotyViewHolder.mUserName.setText(MFPApplication.getInstance().getUser().getUsername() + "(" + MFPApplication.getInstance().getUser().getStudentname() + ")");
//                    topCategotyViewHolder.mUserAge.setText(TimeUtils.getAge(MFPApplication.getInstance().getUser().getStudentbirthdatetime()));
//                    if (TextUtils.isEmpty(MFPApplication.getInstance().getUser().getStudentsex()) || MFPApplication.getInstance().getUser().getStudentsex().equals("0")) {
//                        topCategotyViewHolder.mUserSex.setText("男");
//                    } else {
//                        topCategotyViewHolder.mUserSex.setText("女");
//                    }
//                    topCategotyViewHolder.mUserIntegral.setText("积分:" + MFPApplication.getInstance().getUser().getIntegral());
                    topCategotyViewHolder.mOrderName.setText("当前套餐:" + MFPApplication.getInstance().getUser().getLastestorderproductname());
                    if (MFPApplication.getInstance().getUser().getDepartlist() != null && MFPApplication.getInstance().getUser().getDepartlist().size() > 0) {

//                            topCategotyViewHolder.mProfileOrganization.setText("所在机构:" + getSelectedDepartName(MFPApplication.getInstance().getUser().getDepartlist()));

                            topCategotyViewHolder.mProfileOrganization.setText("所在机构:" + getSelectedDepartName2(MFPApplication.getInstance().getUser().getDepartlist()));


                    }

                    topCategotyViewHolder.mAvator.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mMainMenuCallBack != null) {
                                mMainMenuCallBack.onUserInfoClick();
                            }
                        }
                    });
                    topCategotyViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mMainMenuCallBack != null) {
                                mMainMenuCallBack.onUserInfoClick();
                            }
                        }
                    });
                    topCategotyViewHolder.mChangeOrganization.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mMainMenuCallBack != null) {
                                mMainMenuCallBack.onChangeOrganization();
                            }
                        }
                    });

                } else {
                    topCategotyViewHolder.mAvator.setImageResource(R.mipmap.ic_header_default);
                }
                break;
            case TYPE_CATEGORY:
                CategoryViewHolder categoryViewHolder = (CategoryViewHolder) holder;
                final MainMenuEntity mainMenuEntity = mDataList.get(position - 1);
                categoryViewHolder.mTitle.setText(mDataList.get(position - 1).getmMenuName());
                categoryViewHolder.mIcon.setImageResource(mDataList.get(position - 1).getmMenuIcon());
                if (mCurrentPosition == position) {
                    categoryViewHolder.itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_side_bg_choose));
                } else {
                    categoryViewHolder.itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_transparent));
                }
                categoryViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mCurrentPosition = position;
                        notifyDataSetChanged();
                        if (mMainMenuCallBack != null) {
                            mMainMenuCallBack.onMenuClick(mainMenuEntity);
                        }
                    }
                });
                //消息的位置
                if (mainMenuEntity.getmMenuName() == R.string.main_menu_title_msg) {
                    if (PushMsgCacheCenter.getInstance().getmCurrentUnreadCount() > 0) {
                        categoryViewHolder.mMsgCount.setVisibility(View.VISIBLE);
                        if (PushMsgCacheCenter.getInstance().getmCurrentUnreadCount() >= 100) {
                            categoryViewHolder.mMsgCount.setText("99+");
                        } else {
                            categoryViewHolder.mMsgCount.setText(PushMsgCacheCenter.getInstance().getmCurrentUnreadCount() + "");
                        }

                    } else {
                        categoryViewHolder.mMsgCount.setVisibility(View.GONE);
                    }
                } else {
                    categoryViewHolder.mMsgCount.setVisibility(View.GONE);
                }
                break;
        }
    }

    public String getSelectedDepartName(List<UserResponse.DepartBean> departBeanList) {
        if (departBeanList != null && departBeanList.size() > 0) {
            for (UserResponse.DepartBean departBean : departBeanList) {
                if (departBean.isSelected()) {
                    return departBean.getDepartname() + "(" + departBean.getOrgcode() + ")";
                }
            }
        } else {
            return "";
        }
        return departBeanList.get(0).getDepartname() + "(" + departBeanList.get(0).getOrgcode() + ")";
    }
 public String getSelectedDepartName2(List<UserResponse.DepartBean> departBeanList) {
        if (departBeanList != null && departBeanList.size() > 0) {
            for (UserResponse.DepartBean departBean : departBeanList) {
                if (departBean.isSelected()) {
                    PreferencesStorageUtil.setDepartname(mContext ,departBean.getDepartname());
                    return departBean.getDepartname()  + "\n"+"        (" + departBean.getOrgcode() + ")";
                }
            }
        } else {
            return "";
        }

     PreferencesStorageUtil.setDepartname(mContext ,departBeanList.get(0).getDepartname());
        return departBeanList.get(0).getDepartname() + "\n"+ "         (" + departBeanList.get(0).getOrgcode() + ")";
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        } else {
            return TYPE_CATEGORY;
        }
    }

    public void setChoosedPosition(int position) {
        this.mCurrentPosition = position;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (this.mDataList == null || mDataList.size() == 0) {
            return 1;
        } else {
            return mDataList.size() + 1;
        }
    }

    class TopCategotyViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.avator)
        CircleImageView mAvator;
        @InjectView(R.id.profile_user_name)
        TextView mUserName;
        @InjectView(R.id.profile_order_last_name)
        TextView mOrderName;
        @InjectView(R.id.profile_organization)
        TextView mProfileOrganization;
        @InjectView(R.id.change_organization)
        TextView mChangeOrganization;
        @InjectView(R.id.ll_layout_meal)
        LinearLayout mMeal;
        @InjectView(R.id.ll_layout_organization)
        LinearLayout mOrganization;
//        @InjectView(R.id.main_user_sex)
//        TextView mUserSex;
//        @InjectView(R.id.main_user_age)
//        TextView mUserAge;
//        @InjectView(R.id.main_user_integral)
//        TextView mUserIntegral;


        public TopCategotyViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    class CategoryViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.title)
        TextView mTitle;
        @InjectView(R.id.icon)
        ImageView mIcon;
        @InjectView(R.id.msg_count)
        TextView mMsgCount;

        public CategoryViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public static interface MainMenuCallBack {
        public void onMenuClick(MainMenuEntity mainMenuEntity);

        public void onUserInfoClick();

        public void onChangeOrganization();
    }

}
